package com.ambulance.project.biz.controller.manager;

import com.ambulance.common.utils.SecurityUtils;
import com.ambulance.common.utils.poi.ExcelUtil;
import com.ambulance.framework.aspectj.lang.annotation.Log;
import com.ambulance.framework.aspectj.lang.enums.BusinessType;
import com.ambulance.framework.web.controller.BaseController;
import com.ambulance.framework.web.domain.AjaxResult;
import com.ambulance.framework.web.page.TableDataInfo;
import com.ambulance.project.manager.domain.BizDoctor;
import com.ambulance.project.manager.mapper.BizDoctorMapper;
import com.ambulance.project.manager.service.IBizDoctorService;
import com.ambulance.project.manager.service.impl.BizDoctorServiceImpl;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * 医生管理Controller
 *
 * @author zr
 * @date 2020-05-11
 */
@RestController
@RequestMapping("/manager/doctor")
public class BizDoctorController extends BaseController {
    @Autowired
    private IBizDoctorService bizDoctorService;

    @Autowired
    private BizDoctorMapper bizDoctorMapper;

    /**
     * 查询医生管理列表
     */
    //@PreAuthorize("@ss.hasPermi('manager:doctor:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizDoctor bizDoctor) {
        startPage();
        List<BizDoctor> list = bizDoctorService.selectBizDoctorList(bizDoctor);
        return getDataTable(list);
    }


    /**
     * 导出医生管理列表
     */
    //@PreAuthorize("@ss.hasPermi('manager:doctor:export')")
    @Log(title = "医生管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BizDoctor bizDoctor) {
        List<BizDoctor> list = bizDoctorService.selectBizDoctorList(bizDoctor);
        ExcelUtil<BizDoctor> util = new ExcelUtil<BizDoctor>(BizDoctor.class);
        return util.exportExcel(list, "doctor");
    }

    /**
     * 获取医生管理详细信息
     */
    //@PreAuthorize("@ss.hasPermi('manager:doctor:query')")
    @GetMapping(value = "/{doctorID}")
    public AjaxResult getInfo(@PathVariable("doctorID") Long doctorID) {
        return AjaxResult.success(bizDoctorService.selectBizDoctorById(doctorID));
    }

    /**
     * 新增医生管理
     */
    //@PreAuthorize("@ss.hasPermi('manager:doctor:add')")
    @Log(title = "医生管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizDoctor bizDoctor) {
        return toAjax(bizDoctorService.insertBizDoctor(bizDoctor));
    }



    @PostMapping("/modifyPass")
    @ResponseBody
    public AjaxResult modifyPass(@RequestBody Map<String, Object> params)
    {
        String password = params.get("password").toString();
        String confirm_password = params.get("confirm_password").toString();
        Integer doctorID = Integer.parseInt(params.get("doctorID").toString());

        String pattern = "^[a-z0-9]{6,18}$";
        boolean isMatch = Pattern.matches(pattern, password);
        if (!isMatch) {
            return AjaxResult.error("密码格式不正确");
        }
        if (!password.equals(confirm_password)) {
            return AjaxResult.error("两次密码不正确");
        }

        BizDoctor bizDoctor = new BizDoctor();
        bizDoctor.setDoctorID(doctorID.longValue());
        bizDoctor.setPassword(SecurityUtils.encryptPassword(password));
        int i = bizDoctorMapper.updateBizDoctor(bizDoctor);
        if (i > 0) {
            return AjaxResult.success("ok");
        }
        return AjaxResult.error("系统错误");
    }


    /**
     * 修改医生管理
     */
    //@PreAuthorize("@ss.hasPermi('manager:doctor:edit')")
    @Log(title = "医生管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizDoctor bizDoctor) {
        return toAjax(bizDoctorService.updateBizDoctor(bizDoctor));
    }

    /**
     * 删除医生管理
     */
    //@PreAuthorize("@ss.hasPermi('manager:doctor:remove')")
    @Log(title = "医生管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{doctorIDs}")
    public AjaxResult remove(@PathVariable Long[] doctorIDs) {
        return toAjax(bizDoctorService.deleteBizDoctorByIds(doctorIDs));
    }

    /**
     * 冻结医生
     */
//    //@PreAuthorize("@ss.hasPermi('manager:doctor:remove')")
//    @Log(title = "医生管理", businessType = BusinessType.DELETE)
    @PutMapping("light/{doctorID}")
    public AjaxResult light(@PathVariable Long doctorID) {
        return toAjax(bizDoctorService.lightBizDoctor(doctorID));
    }
}
